package com.itheima.controller.servlet;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.domain.QuestionItem;
import com.itheima.service.QuestionItemService;
import com.itheima.service.impl.QuestionItemServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/itemServlet")
public class QuestionItemServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        String method = request.getParameter("method");
        Map resultMap = null;
        if ("delete".equals(method)){
            resultMap = this.delete(request,response);
        } else if ("findAll".equals(method)){
            resultMap = this.findAll(request,response);
        } else if ("update".equals(method)){
            resultMap = this.update(request,response);
        }


        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(resultMap);
        response.getWriter().write(json);
    }

    private Map update(HttpServletRequest request, HttpServletResponse response) {
        Map resultMap = new HashMap();
        try {
            String questionId = request.getParameter("questionId");
            ServletInputStream inputStream = request.getInputStream();
            ObjectMapper objectMapper = new ObjectMapper();
            List<QuestionItem> list = objectMapper.readValue(inputStream, new TypeReference<List<QuestionItem>>() {});
            service.update(questionId,list);
            resultMap.put("flag",true);
        } catch (IOException e) {
            resultMap.put("flag",false);
            resultMap.put("message","更新失败!");
            e.printStackTrace();
        }
        return resultMap;
    }

    private Map findAll(HttpServletRequest request, HttpServletResponse response) {
        Map resultMap = new HashMap();
        try {
            String questionId = request.getParameter("qid");
            List<QuestionItem> list =service.findAll(questionId);
            resultMap.put("flag",true);
            resultMap.put("resultData",list);
        } catch (Exception e) {
            resultMap.put("flag",false);
            resultMap.put("message","查询失败!");
            e.printStackTrace();
        }
        return resultMap;
    }

    QuestionItemService service = new QuestionItemServiceImpl();
    private Map delete(HttpServletRequest request, HttpServletResponse response) {
        Map resultMap = new HashMap();
        try {
            String questionId = request.getParameter("questionId");
            service.delete(questionId);
            resultMap.put("flag",true);
        } catch (Exception e) {
            resultMap.put("flag",false);
            resultMap.put("message","删除失败!");
            e.printStackTrace();
        }
        return resultMap;
    }
}
